package cn.stronglink.asset.module.sys.department.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.plugins.Page;

import cn.stronglink.asset.model.Department;
import cn.stronglink.asset.module.sys.department.service.DepartmentService;
import cn.stronglink.asset.module.sys.department.vo.DepartmentTreeNodeVo;
import cn.stronglink.suite.core.base.AbstractController;
import cn.stronglink.suite.core.config.Resources;
import cn.stronglink.suite.core.log.annotation.OperateLog;
import cn.stronglink.suite.core.log.annotation.OperateLog.OpType;
import cn.stronglink.suite.core.support.HttpCode;

@Controller
@RequestMapping(value = "sys/dept")
public class DepartmentController extends AbstractController {

	@Autowired
	private DepartmentService departmentService;

	@RequestMapping(value = "qryDepts")
	public ResponseEntity<ModelMap> qryDepts(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response) {
		List<DepartmentTreeNodeVo> vo = departmentService.getTree();
		return setSuccessModelMap(modelMap, vo);
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "qryDeptsByPid")
	public ResponseEntity<ModelMap> qryDeptsByPid(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Map<String, Object> params) {
		Long pid = null;
		if(params.get("pid") != null){
			pid = Long.valueOf(params.get("pid").toString());
		}
		String name = null;
		if(params.get("name") != null){
			name = params.get("name").toString();
		}	
		Page<Department> page = (Page<Department>) super.getPage(params);
		Page<Department> data = departmentService.getDeptsByPid(page, pid, name);
		return setSuccessModelMap(modelMap, data);
	}
	
	@OperateLog(module = "组织机构",desc="添加组织机构数据", type = OpType.ADD)
	@RequestMapping(value = "create")
	public ResponseEntity<ModelMap> create(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Department dept) {
		departmentService.insertDept(dept);
		return setSuccessModelMap(modelMap, null);
	}
	
	@OperateLog(module = "组织机构",desc="修改组织机构数据", type = OpType.UPDATE)
	@RequestMapping(value = "edit")
	public ResponseEntity<ModelMap> edit(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Department dept) {
		departmentService.updateDept(dept);
		return setSuccessModelMap(modelMap, null);
	}
	
	@OperateLog(module = "组织机构",desc="删除组织机构数据", type = OpType.DEL)
	@RequestMapping(value = "remove")
	public ResponseEntity<ModelMap> remove(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Long id) {
		
		int count = departmentService.getChildDetpCount(id);
		if(count > 0){		
			return setModelMap(modelMap, HttpCode.CONFLICT, Resources.getMessage("HAVECHILDDEPT"));
		}
		departmentService.removeDept(id);
		return setSuccessModelMap(modelMap, null);
	}

}
